package event;

import java.io.OutputStream;

import stream.DebugStream;

/**
 * A utility class for logging and writing debug messages.
 * @author Charles
 *
 */
public class DebugLogger extends Logger implements DebugStream {
	private boolean active;
	
	/**
	 * Creates a new debug logger that writes to an output stream.
	 * @param o the stream to which the new logger writes.
	 * @param active if the logger should write to its stream by default.
	 */
	public DebugLogger(OutputStream o, boolean active) {
		super(o);
		this.active = active;
	}
	
	/**
	 * Returns if this logger is currently set to write to its stream.
	 * @return <code>true</code> if this logger is currently set to write to 
	 * its stream.
	 */
	public boolean isActive() {
		return active;
	}
	
	/**
	 * Sets whether this logger should write to its stream when a message is logged. 
	 * <p>When the logger is active, it will attempt to write any message as soon as it is 
	 * logged to its stream. When the logger is not active, messages that are logged still 
	 * increase the message count but will not be written to the stream. When the logger 
	 * is set to active after being in the inactive state, any messages that were logged 
	 * while it was inactive will <i>not</i> be written to the stream.
	 * @param b if this logger should write to its stream when a message is logged.
	 */
	public void setActive(boolean b) {
		this.active = b;
	}
	
	/**
	 * Convenience method for {@link #logMsg(String)}.
	 * @param s the message to log.
	 */
	public void logDebugMsg(final String s) {
		logMsg(s);
	}
	
	/**
	 * Logs a message. The message will be written to the stream only if this logger is 
	 * active.
	 */
	@Override
	public void logMsg(final String s) {
		++n;
		if (isActive()) {
			writeToOutput(s);
		}
	}

	@Override
	public void write(String s) {
		logMsg(s);
	}
}
